package main

import (
	"fmt"
	"sort"
)



type byLength []string

func (s byLength) Len() int {
	return len(s)
}
func (s byLength) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s byLength) Less(i, j int) bool {
	return len(s[i]) < len(s[j])
}



func main() {

	// ------------------- 针对builtin type内置类型排序 --------------------------

	strs := []string{"c", "a", "b","af","ab","aa"}
	sort.Strings(strs)
	fmt.Println("Strings:", strs)   // Strings: [a aa ab af b c]

	ints := []int{7, 2, 4}
	sort.Ints(ints)
	fmt.Println("Ints:   ", ints)  // Ints:    [2 4 7]

	s := sort.IntsAreSorted(ints)   // 判断整型切片 是否按增序排列
	fmt.Println("Sorted: ", s)  // Sorted:  true

	// ------------------ 针对自定义排序规则 --------------------------------------
	fruits := []string{"peach", "banana", "kiwi"}
	sort.Sort(byLength(fruits))
	fmt.Println(fruits)

}